package com.qpx.zhoukaofile.service;


import com.qpx.zhoukaofile.dao.FileDao;
import com.qpx.zhoukaofile.entity.FileEntity;
import com.zhoukao.dto.FileDto;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FileService {

    @Autowired
    FileDao fileDao;

    public void save(List<FileDto> fileDto){
        List<FileEntity> list = new ArrayList<FileEntity>();
        for (FileDto dto : fileDto) {
            FileEntity fileEntity = new FileEntity();
            BeanUtils.copyProperties(dto,fileEntity);
            list.add(fileEntity);
        }
        fileDao.saveAll(list);
    }


    public List<FileEntity> findFileByMid(int mid){
        List<FileEntity> byMid = fileDao.findByMid(mid);

        return byMid;
    }

    public void delFileByID(List<String> ids) {

        List<FileEntity> list = new ArrayList<>();
        for (String id : ids) {
            FileEntity fileEntity = new FileEntity();
            fileEntity.setFid(Integer.valueOf(id));
            list.add(fileEntity);
        }
        fileDao.deleteAll(list);
    }

    public void delFileByMID(List<String> mids) {
        List<Integer> integers = mids.stream().map(Integer::parseInt).collect(Collectors.toList());

        fileDao.deleteAllByMid(integers);
    }
}
